package com.pucong.gitee.database.backup.business.database.service;

import com.pucong.gitee.database.backup.common.selfenum.CommonBoolenEnum;
import com.pucong.gitee.database.backup.business.database.util.OperationService;
import com.pucong.gitee.database.backup.business.database.vo.DatabaseVo;
import com.pucong.gitee.database.backup.business.database.vo.ExeSqlResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Service
public class OperationDataSqlServiceImpl implements OperationDataSqlService {

    @Autowired
    private DatabaseStaticService databaseStaticService;

    @Override
    public ExeSqlResult executeSql(String databaseId, DatabaseVo databaseVo) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sessionId = request.getSession().getId();

        OperationService operationService = databaseStaticService.getConnectionById(sessionId, databaseId);
        String sql = databaseVo.getSql();
        Object[] params = null;
        ExeSqlResult exeSqlResult = new ExeSqlResult();

        if(sql.toLowerCase().contains(" alert ") || sql.toLowerCase().contains(" drop ") ||
                sql.toLowerCase().contains(" update ") ||
                sql.toLowerCase().contains(" insert ") || sql.toLowerCase().contains(" delete ")){
            // 新增、修改、删除
            int i = operationService.executeUpdate(sql, params);
            exeSqlResult.setResultInteger(new BigDecimal(i));
            exeSqlResult.setSessionId(operationService.getSessionId());
        }else{
            // 查询
            if(databaseVo.getPageNo() != null && databaseVo.getPageSize() != null){
                sql = operationService.generatePageSql(sql,databaseVo.getPageNo(),databaseVo.getPageSize());
            }
            exeSqlResult = operationService.executeQuery(sql, params);
        }
        operationService.closeAll();
        return exeSqlResult;
    }

    @Override
    public Boolean autoTransactionalCommit(String databaseId, DatabaseVo databaseVo) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sessionId = request.getSession().getId();

        OperationService operationService = databaseStaticService.getConnectionById(sessionId, databaseId);
        if(CommonBoolenEnum.是 == databaseVo.getCommitTransactional()){
            operationService.commitTransactional();
        }else if(CommonBoolenEnum.是 == databaseVo.getAutoCommit()){
            operationService.falseAutoCommit();
        }else if(CommonBoolenEnum.否 == databaseVo.getAutoCommit()){
            operationService.trueAutoCommit();
        }
        operationService.closeAll();
        return false;
    }
}
